610 - Street directions (Grafos, DFS) && 10986 - Sending email (Grafos, Algoritmo...
[and.git] / 11339 - Sonnet Rhyme Verifier / Sonnet.java
blob553c8e1a76eca1f67b59ae1bda71f580caf23553
1 import java.util.*;
2 import java.io.*;
3 import java.math.*;
5 class Main{
7 public static String clean(String s){
8 String r = "";
9 s = s.toLowerCase();
10 for (int i=0; i<s.length(); ++i){
11 if (Character.isLetter(s.charAt(i))){
12 r += s.charAt(i);
15 if (r.charAt(r.length()-1) == 's'){
16 r = r.substring(0, r.length()-1);
18 return r;
21 public void procesarCaso(ArrayList<String> p, String s){
22 HashMap<String, Character> m = new HashMap<String, Character>();
23 String sufijos[] = s.split(" ");
24 String result = "";
25 boolean ilegal = false;
26 char sufijoActual = 'A';
27 for (int i=0; i<p.size(); ++i){
28 boolean rimo = false;
29 for (int j=0; j<sufijos.length; ++j){
30 if (p.get(i).endsWith(sufijos[j])){
31 //System.out.println(p.get(i) + " rima con " + sufijos[j]);
32 rimo = true;
33 if (m.containsKey(sufijos[j])){
34 result += m.get(sufijos[j]);
35 }else{
36 m.put(sufijos[j], Character.valueOf(sufijoActual));
37 result += sufijoActual;
38 sufijoActual++;
42 if (!rimo){
43 ilegal = true;
47 //System.out.println(result);
49 if (ilegal){
50 System.out.println("Not a chance!");
51 return;
53 if (result.equals("ABBAABBACDECDE") || result.equals("ABBAABBACDEDCE") || result.equals("ABBAABBACDCDCD")){
54 System.out.println(result);
55 return;
57 System.out.println("Not a chance!");
61 public static void main(String[] args) throws IOException{
63 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
64 String line = reader.readLine();
65 while (line != null){
66 String sufijos = line;
67 line = reader.readLine();
68 System.out.print(line + ": "); //titulo
69 //ahora leamos el cuerpo del poema
70 line = reader.readLine();
71 ArrayList<String> p = new ArrayList<String>();
72 while (line != null && line.trim().length() > 0){
73 //System.out.println("Read " + line);
74 p.add(clean(line));
75 //System.out.println("Added " + p.get(p.size()-1));
76 line = reader.readLine();
78 Main s = new Main();
79 s.procesarCaso(p, sufijos);
81 line = reader.readLine(); //leer sufijos del siguiente caso